package engine;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import map.Map;
import map.Tile;
import objects.Creature;
import time.TimeQueue;
import view.View;

/**
 * This class extends the engine, and implements the finer details of the Game.
 * 
 * @author Kyle_Solo
 */
public class Game extends GameEngine {

	/**
	 * The entry point into the code.
	 * 
	 * @param args
	 *            command line arguments
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		Game game = new Game();
		game.run(args, 400, 400);
	}

	private Creature c;

	/**
	 * The current map
	 */
	public Map map;

	private int mouseX;
	private int mouseY;

	/**
	 * The list of GameObjects that are waiting their turn to take some action
	 */
	public TimeQueue time;

	/**
	 * The main view
	 */
	public View view;

	@Override
	public int gameLoop() {
		init();
		boolean running = true;
		while (running) {
			draw();
			running = logic();
		}
		return 0;
	}

	private void init() {
		time = new TimeQueue();

		map = new Map(100, 100);
		view = new View(map, 20, 20, display.getWidth() - 40, display.getHeight() - 40, 48, 48);
		Tile start = map.getTile(0, 0);
		c = new Creature();
		time.add(c);
		start.addObject(c);
	}

	private boolean logic() {
		return true;
	}

	/**
	 * This method lets the player control a Creature's actions using mouse/keyboard input. The
	 * creature being controlled doesn't matter, any creature can be controlled with this method.
	 * 
	 * @param creature
	 *            the creature being controlled
	 * @return true if the player's turn is over
	 */
	public boolean playerTurn(Creature creature) {
		view.viewCenter(creature.currentTile);
		// handle input
		InputEvent ie = getInput();

		if (ie != null) {
			if (ie.getClass() == KeyEvent.class) {
				KeyEvent ke = (KeyEvent) ie;
				// keyboard input
				switch (ke.getKeyCode()) {
				case KeyEvent.VK_W:
					return creature.move(Map.DIRECTION.N);
				case KeyEvent.VK_E:
					return creature.move(Map.DIRECTION.NE);
				case KeyEvent.VK_D:
					return creature.move(Map.DIRECTION.E);
				case KeyEvent.VK_C:
					return creature.move(Map.DIRECTION.SE);
				case KeyEvent.VK_X:
					return creature.move(Map.DIRECTION.S);
				case KeyEvent.VK_Z:
					return creature.move(Map.DIRECTION.SW);
				case KeyEvent.VK_A:
					return creature.move(Map.DIRECTION.W);
				case KeyEvent.VK_Q:
					return creature.move(Map.DIRECTION.NW);
				case KeyEvent.VK_ESCAPE:
					System.exit(0);
					return false;
				}
			} else if (ie.getClass() == MouseEvent.class) {
				MouseEvent me = (MouseEvent) ie;
				// mouse input
				switch (me.getID()) {
				case MouseEvent.MOUSE_MOVED:
					mouseX = me.getX();
					mouseY = me.getY();
					break;
				case MouseEvent.MOUSE_PRESSED:
					mouseX = me.getX();
					mouseY = me.getY();
					System.out.println("Mouse Clicked at: (" + mouseX + "," + mouseY + ")");
					// TODO: mouse driven menu system
					break;
				}
			}
		}
		return false;
	}

	@Override
	public void render(Graphics2D g2d) {
		g2d.setColor(Color.black);
		g2d.drawRect(0, 0, 399, 399);
		view.render(g2d, mouseX, mouseY);
	}
}